<template>
    <div class="FromGood">
        <el-form :model="foodFrom" ref="goodFrom" label-width="120px" class="demo-ruleForm">
            <el-form-item label="菜品名称" prop="name">
                <el-input v-model="foodFrom.foodName" placeholder="请输入产品名称"></el-input>
            </el-form-item>
            <el-form-item label="菜品描述" prop="discription">
                <el-input type="textarea" :rows="3" v-model="foodFrom.foodDescription"></el-input>
            </el-form-item>
            <el-form-item label="菜品原料" prop="material">
                <el-input type="textarea" :rows="4" v-model="foodFrom.foodMaterial"></el-input>
            </el-form-item>
            <el-form-item label="制作步骤" prop="step">
                <el-input type="textarea" :rows="9" v-model="foodFrom.step"></el-input>
            </el-form-item>
            <el-form-item label="宣传图" prop="region">
                <el-upload action="#" ref="upload" :on-preview="handlePreview" :on-remove="handleRemove"
                    list-type="picture-card" :file-list="fileList" :http-request="handleFileUpload">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="updateDetail(1)">草稿</el-button>
                <el-button type="primary" @click="updateDetail(2)">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { AlbumUpload,deleteFile} from '@/assets/api/fileUpload.js';
import { selectFoodDetail, updateFoodDetail } from '@/assets/api/food.js'
export default {
    data() {
        return {
            imageList: [],
            fileList: [],
            foodId: '',
            foodFrom: {
                foodName: '',
                foodDescription: '',
                foodMaterial: '',
                step: '',
                foodImg: '',
                foodType: 1,
                status:0
            },

            dialogImageUrl: '',
            dialogVisible: false

        }
    },
    mounted() {
        this.foodId = this.$route.params.foodId
        if (this.foodId != undefined && this.foodId != '') {
            this.showDetail()
        }
    },
    methods: {

        async showDetail() {
            selectFoodDetail(this.foodId).then((resp) => {
                if (resp.code == 200) {
                    this.foodFrom = resp.data;
                    if (this.foodFrom.foodImg != '') {
                        this.imageList = this.foodFrom.foodImg.split(",")
                    }
                    if (resp.data.imageList.length > 0) {
                        for (var i = 0; i < resp.data.imageList.length; i++) {
                            this.fileList.push({ "name": resp.data.imageList[i].pictureId
                            ,"url": resp.data.imageList[i].pictureUrl })
                        }
                    }
                }
            })
        },

        async updateDetail(type) {
            if (this.imageList.length > 0) {
                this.foodFrom.foodImg =  this.imageList.toString();
            }
            this.foodFrom.status=type;
            updateFoodDetail(this.foodFrom).then((resp) => {
                if (resp.code == 200) {
                    //关闭当前卡签页
                    this.$store.dispatch("delVisitedViews", this.$route)
                    this.$router.push("/control/homeCooking")
                }
            })
        },
        // 处理移除操作
        handleRemove(file, fileList) {
            var fileName=file.name;
            deleteFile(fileName).then((resp)=>{
                if(resp.code==200){
                    this.imageList=this.imageList.filter(item=>item!=fileName)
                }
            })
        },
        // 处理预览操作
        handlePreview(file) {
            console.log(file);
        },
        // 处理文件上传操作
        handleFileUpload(file) {
            this.loading = true;
            var form = new FormData();
            // 添加键值对
            form.append("file", file.file);
            // 调用后端服务器的接口
            AlbumUpload(form).then((resp) => {
                this.dialogImageUrl = resp.data.pictureUrl;
                this.imageList.push(resp.data.pictureId)
               
            }).catch((e) => {
                this.$message.error(e.message);
                this.$refs.upload.clearFiles();
            })
        }
    }
}
</script>

<style scoped>
.FromGood {
    width: 100%;
    min-height: 100%;
    padding: 15px;
    box-sizing: border-box;
}
</style>